#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstdint>
#include <ctime>
#include <stack>
#include <string>

using namespace std;

class Solution {
public:
    vector<string> test;
    string simplifyPath(string path) {
        stack<char> sta;
        int pointCnt = 0;
        int wordFlag = 0;
        sta.push('/');
        for (int i = 0; i < path.length(); i++){
            if (path[i] == '/'){
                if (0 == pointCnt){
                    if (sta.top() != '/'){
                        sta.push(path[i]);
                    }
                }else if (1 == pointCnt){
                    sta.pop();
                    // if (sta.size() > 1){
                    //     sta.pop();
                    // }
                }else if (2 == pointCnt){
                    sta.pop();
                    sta.pop();
                    if (sta.size() > 1){
                        sta.pop();
                    }
                    while(sta.top() != '/'){
                        sta.pop();
                    }
                }else {
                    sta.push(path[i]);
                }
                pointCnt = 0;
                wordFlag = 0;
            }else if (path[i] == '.'){
                sta.push(path[i]);
                if (!wordFlag){
                    pointCnt++;
                }
            }else{
                sta.push(path[i]);
                wordFlag = 1;
                pointCnt = 0;
            }
        }

        if (sta.size() > 1 && sta.top() == '/'){
            sta.pop();
        }else if (1 == pointCnt){
            sta.pop();
            if (sta.size() > 1){
                sta.pop();
            }
        }else if (2 == pointCnt){
            sta.pop();
            sta.pop();
            if (sta.size() > 1){
                sta.pop();
            }
            while(sta.top() != '/'){
                sta.pop();
            }
            if (sta.size() > 1){
                sta.pop();
            }
        }

        vector<char> vec;
        while(!sta.empty()){
            vec.push_back(sta.top());
            sta.pop();
        }
        string str;
        for (int i = vec.size()-1; i >= 0; i--){
            str.push_back(vec[i]);
        }
        
        return str;
    }
};

int main()
{
    Solution su;
    su.test.push_back("/a/../../b/../c//.//");
    su.test.push_back("/a//b////c/d//././/..");
    su.test.push_back("/a/./b/../../c/");
    su.test.push_back("/../");
    su.test.push_back("/home/");
    su.test.push_back("/...");
    su.test.push_back("/..hidden");
    for (int i = 0; i < su.test.size(); i++){
        string result = su.simplifyPath(su.test[i]);
        cout << result << endl;
    }
    return 0;
}
